function A = slrandsym(eigvals)
%SLRANDSYM Generates a symmetric matrix with specified eigenvalues randomly
%
% [ Syntax ]
%   - A = slrandsym(eigvals)
%
% [ Description ]
%   - A = slrandsym(eigvals) randomly generates a symmetric matrix, whose
%     eigenvalues are equal to thos specified in eigvalues. 
%
%     eigvals should be a vector with length d. Then A will be a d x d
%     matrix.
%
% [ History ]
%   - Created by Dahua Lin, on Dec 23, 2007
%

%% parse and verify input arguments

assert(isnumeric(eigvals) && isvector(eigvals), ...
    'sltoolbox:slrandsym:invalidarg', ...
    'eigvals should be a numeric vector.');

%% main 

d = length(eigvals);
R = slrandorth(d);

if size(eigvals, 1) > 1
    eigvals = eigvals';
end

A = bsxfun(@times, R, eigvals) * R';

A = (A + A') * 0.5;